package ams.action;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ModelDriven;

import ams.model.entity.Rights;
import ams.service.IRightsService;
import ams.util.RightsType;

@Namespace("/rights")
public class RightsAction extends BaseAction<Rights> implements ModelDriven<Rights> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private IRightsService rightsService;

	/**
	 * 接收的参数
	 */
	private Rights rights = new Rights();

	/**
	 * 返回的json对象
	 */
	private List<Rights> rightsList = new ArrayList<>();
	private RightsType[] rightsTypeArray;

	
	@Action(value="rightsAdmin",results={@Result(location="/WEB-INF/jsp/rights.jsp")})
	public String rightsAdmin(){
		return SUCCESS;
	}
	
	// 添加权限
	@Action(value = "add", results = { @Result(type = "json", params = { "root", "execResult", "excludeNullProperties",
			"true", "contentType", "text/html" }) })
	public String add() {
		super.setExecResult(rightsService.add(rights));
		return SUCCESS;
	}

	// 删除权限
	@Action(value = "delete", results = {
			@Result(type = "json", params = { "root", "execResult", "excludeNullProperties", "true" }) })
	public String delete() {
		super.setExecResult(rightsService.delete(rights));
		return SUCCESS;
	}

	// 更新权限
	@Action(value = "update", results = { 
			@Result(type = "json", params = { "root", "execResult","excludeNullProperties", "true", "contentType", "text/html" }) })
	public String update() {
		super.setExecResult(rightsService.update(rights));
		return SUCCESS;
	}

	// 权限treeGrid;父权限combotree
	@Action(value = "getRightsByPid", results = {
			@Result(type = "json", params = { "root", "rightsList", "excludeNullProperties", "true" }) })
	public String getRightsByPid() {
		this.rightsList = rightsService.getRightsByPid(rights.getId());
		return SUCCESS;
	}

	// 角色赋权的权限树
	@Action(value = "getRightsCatalog", results = {
			@Result(type = "json", params = { "root", "rightsList", "excludeNullProperties", "true" }) })
	public String getRightsCatalog() {
		rightsList = rightsService.getAllRights();
		return SUCCESS;
	}

	// 权限类型combobox
	@Action(value = "getRigthsType", results = {
			@Result(type = "json", params = { "root", "rightsTypeArray", "enumAsBean", "true" }) })
	public String getRigthsType() {
		this.rightsTypeArray = RightsType.values();
		return SUCCESS;
	}

	public IRightsService getRightsService() {
		return rightsService;
	}

	public void setRightsService(IRightsService rightsService) {
		this.rightsService = rightsService;
	}

	public Rights getRights() {
		return rights;
	}

	public void setRights(Rights rights) {
		this.rights = rights;
	}

	public List<Rights> getRightsList() {
		return rightsList;
	}

	public void setRightsList(List<Rights> rightsList) {
		this.rightsList = rightsList;
	}

	public RightsType[] getRightsTypeArray() {
		return rightsTypeArray;
	}

	public void setRightsTypeArray(RightsType[] rightsTypeArray) {
		this.rightsTypeArray = rightsTypeArray;
	}

	@Override
	public Rights getModel() {
		return rights;
	}

}
